package com.isunland.managesystem.adapter;

import android.support.annotation.NonNull;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseButterKnifeAdapter;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.SimpleTreeListParams;
import com.isunland.managesystem.utils.LocalSearch;
import com.isunland.managesystem.utils.MyStringUtil;
import java.util.ArrayList;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/5/18 17:06
 */

public class SimpleTreeAdapter<T> extends BaseButterKnifeAdapter<T> {
  private final CallBack mCallBack;
  private SimpleTreeListParams mParams;
  private T mCheckedItem;

  public SimpleTreeAdapter(BaseVolleyActivity context, ArrayList<T> objects,
      SimpleTreeListParams params, CallBack callback) {
    super(context, objects);
    this.mParams = params;
    this.mCallBack = callback;
  }

  public T getCheckedItem() {
    return mCheckedItem;
  }

  public void setCheckedItem(T mCheckedItem) {
    this.mCheckedItem = mCheckedItem;
  }

  @Override protected int setLayoutId() {
    return R.layout.adapter_tree;
  }

  @NonNull @Override protected BaseViewHolder initHolder(View convertView) {
    return new ViewHolder(convertView);
  }

  @Override protected void initView(T item, BaseViewHolder baseHolder, int position) {
    ViewHolder holder = (ViewHolder) baseHolder;
    holder.tvTitleTree.setText(LocalSearch.getFieldValue(item, mParams.getTitleField()));
    String content = LocalSearch.getFieldValue(item, mParams.getContentField());
    holder.tvContentTree.setText(content);
    holder.tvContentTree.setVisibility(MyStringUtil.isEmpty(content) ? View.GONE : View.VISIBLE);
    holder.cbAll.setTag(position);
    holder.cbAll.setChecked(mCheckedItem != null && mCheckedItem.equals(item));
    //holder.cbAll.setChecked(((ICheckable)item).getIChecked());

  }

  public interface CallBack {
    void refresh();
  }

  protected class ViewHolder extends BaseViewHolder {
    @BindView(R.id.iv_logo) ImageView ivLogo;
    @BindView(R.id.tv_title_tree) TextView tvTitleTree;
    @BindView(R.id.tv_content_tree) TextView tvContentTree;
    @BindView(R.id.cb_all) CheckBox cbAll;

    ViewHolder(View view) {
      super(view);
      cbAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override public void onCheckedChanged(CompoundButton v, boolean isChecked) {
          if (v.getTag() == null) return;
          int position = (int) v.getTag();
          T item = getItem(position);
          if (isChecked) {
            mCheckedItem = item;
            mCallBack.refresh();
          }
          if ((!isChecked) && (mCheckedItem != null && mCheckedItem.equals(item))) {
            mCheckedItem = null;
            mCallBack.refresh();
          }
          //mCheckedItem = isChecked ? item : null;
          //((ICheckable)item).setIChecked(isChecked);

        }
      });
    }
  }
}
